package cn.webdai.swagger.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 戴金华
 * @date 2020-01-02 9:55
 */
@Configuration
@EnableSwagger2     //开启swagger的功能
public class SwaggerConfig {

    //是否开启swagger 正式环境一般需要关闭
    @Value("${swagger.enabled}")
    private boolean enableSwagger;


    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启(true开启 false 隐藏 生产环境建议隐藏)
                .enable(enableSwagger)
                .select()
                //扫描的包路径 设置basePackages会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage("cn.webdai.swagger.controller"))
                //指定路径处理    PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("SpringBoot整合swagger构建RESTful接口文档")
                .description("接口说明")
                .termsOfServiceUrl("http://127.0.0.1:8080")
                .contact("dai15")
                .version("1.0.0")
                .build();
    }
}
